import logging, random, json
from model.models import CompertitionRecord

class MapService(object):
    def __init__(self, uuid):
        self.cell_line = 15
        self.user_cells = []
        self.robot_cells = []
        self.uuid = uuid

    def is_victory(self, cells):
        i = 0
        victory = False
        while(i < cells.__len__() and not victory):
            cell = cells[i]
            if(self.add_equlse(cell, 1, 0) or self.add_equlse(cell, 0, 1) or self.add_equlse(cell, 1, 1) or self.add_equlse(cell, -1, 1)):
                victory = True
            i += 1
        return victory

    def add_equlse(self, cell, x_add_num, y_add_num):
        equlse = False
        x_add = 0
        y_add = 0
        j = 1
        whos = set()
        while (j < 5):
            x_add += x_add_num
            y_add += y_add_num
            who_down = self.is_down([cell[0] + x_add, cell[1] + y_add])
            whos.add(who_down)
            if (not (whos.__len__() == 1 and list(whos)[0] != None)):
                break
            if (j == 4):
                equlse = True
            j += 1
        return equlse

    def is_down(self, cell):
        for cell2 in self.user_cells:
            if(cell == cell2):
                return 'user'
        for cell2 in self.robot_cells:
            if(cell == cell2):
                return 'roboot'

    def save(self, user_cells, robot_cells, robot_victory):
        cr = CompertitionRecord(user_cells=user_cells, robot_cells=robot_cells, robot_victory=robot_victory)
        cr.save()

    def clear(self):
        self.user_cells.clear()
        self.robot_cells.clear()

    def get_list(self, page_no, page_size):
        page_first = page_no * page_size
        page_last = page_first + page_size
        return CompertitionRecord.objects.all()[page_first : page_last]

    def get_cell(self):
        optimun = False
        page_no = 0
        is_random = False
        while(not optimun):
            records = self.get_list(page_no, 20)
            for record in records:
                user_cells_len = self.user_cells.__len__()
                robot_cells_len = self.robot_cells.__len__()
                record_user_cells = json.loads(record.user_cells)
                record_robot_cells = json.loads(record.robot_cells)
                if(record.robot_victory):
                    if(not False in [user_cell in record_user_cells for user_cell in self.user_cells]
                            and not False in [robot_cell in record_robot_cells for robot_cell in self.robot_cells]):
                        while (True):
                            cell = random.choice(record_robot_cells)
                            if(cell not in self.robot_cells):
                                break
                        optimun = True
                        break
                else:
                    if(not False in [user_cell in record_robot_cells for user_cell in self.user_cells]
                            and not False in [robot_cell in record_robot_cells for robot_cell in self.robot_cells]):
                        while (True):
                            cell = random.choice(record_user_cells)
                            if(cell not in self.user_cells):
                                break
                        optimun = True
                        break
            if(records.__len__() == 0):
                is_random = True
                cell = [random.randint(1, self.cell_line), random.randint(1, self.cell_line)]
                break
            page_no += 1
        return is_random, cell

